package com.wy.component.file;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class FileUtil {
	public static void copyFile(File sourceFile, File targetFile)
			throws IOException {
		// 新建文件输入流并对它进行缓冲
		FileInputStream input = new FileInputStream(sourceFile);
		BufferedInputStream inBuff = new BufferedInputStream(input);

		// 新建文件输出流并对它进行缓冲
		FileOutputStream output = new FileOutputStream(targetFile);
		BufferedOutputStream outBuff = new BufferedOutputStream(output);

		// 缓冲数组
		byte[] b = new byte[1024 * 5];
		int len;
		while ((len = inBuff.read(b)) != -1) {
			outBuff.write(b, 0, len);
		}

		// 刷新此缓冲的输出流
		outBuff.flush();

		// 关闭流
		inBuff.close();
		outBuff.close();
		output.close();
		input.close();

	}
	
    /*简单说明下，files就是你需要的那个指定类型文件的file对象list，
     *你需要知道具体文件名或者文件路径或者文件大小等，自己通过File的API去取,比如：file.getName();file.getAbsolutePath()……
     *files：不要跟我说你直接传null啊
     *suffixs:文件后缀，注意全是小写，不包含点，比如：rar，text等，支持* 或者空list，后缀lsit为空我就认为是不过滤后缀
     * path：任意路径
     */
 	public static List<File> getAllFiles(List<File> files,
 			List<String> suffixs, String fileNameSuffixs, String path) {
 		File fileT = new File(path);
 		if (fileT.exists()) {
 			if (fileT.isDirectory()) {
 				for (File f : fileT.listFiles()) {
 					files = getAllFiles(files, suffixs, fileNameSuffixs, f.getAbsolutePath());
 				}
 			} else {
 				// 被没后缀的文件坑过，所以加了无后缀文件判断
 				if (suffixs.size() == 0 || suffixs.contains("*") || 
 						(fileT.getName().indexOf(".") != -1 && 
 						suffixs.contains(fileT.getName().split("\\.")[1].toLowerCase()) || fileT.getName().contains(fileNameSuffixs)
 						)
 						) {
 					files.add(fileT);
 				}
 			}
 		}
 		return files;
 	}
 	
 	public static byte[] File2byte(String filePath)
	{
		byte[] buffer = null;
		try
		{
			File file = new File(filePath);
			FileInputStream fis = new FileInputStream(file);
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			byte[] b = new byte[1024];
			int n;
			while ((n = fis.read(b)) != -1)
			{
				bos.write(b, 0, n);
			}
			fis.close();
			bos.close();
			buffer = bos.toByteArray();
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		return buffer;
	}

	public static void byte2File(byte[] buf, String filePath, String fileName)
	{
		BufferedOutputStream bos = null;
		FileOutputStream fos = null;
		File file = null;
		try
		{
			File dir = new File(filePath);
			if (!dir.exists() && dir.isDirectory())
			{
				dir.mkdirs();
			}
			file = new File(filePath + File.separator + fileName);
			fos = new FileOutputStream(file);
			bos = new BufferedOutputStream(fos);
			bos.write(buf);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (bos != null)
			{
				try
				{
					bos.close();
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
			}
			if (fos != null)
			{
				try
				{
					fos.close();
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
			}
		}
	}
	
}
